package com.hrt.datasender.datadao.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.executer.IORMExecuter;
import com.btcode.orm.model.ModelPageResult;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.db.DBManager;

public final class BcSendPlatformService {
    private static volatile BcSendPlatformService instance;

    private BcSendPlatformService() {

    }

    public static BcSendPlatformService getInstance() {
        if (instance == null) {
            synchronized (BcSendPlatformService.class) {
                if (instance == null) {
                    instance = new BcSendPlatformService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取指定发送平台信息
     * 
     * @param ip
     * @param port
     * @return
     */
    public BcSendPlatformModel getPlatformInfo(String ip, String port) {
        String sql = "SELECT * FROM BC_SEND_PLATFORM WHERE IP='" + ip + "' AND PORT='" + port + "'";
        try {
            SQLParameterList pc = new SQLParameterList();
            List<BcSendPlatformModel> list = DBManager.getORMExecuter().getModelListSQL(sql, pc,
                    BcSendPlatformModel.class);
            if (list.size() > 0){
                return list.get(0);
            }     
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取发送平台列表
     * 
     * @return
     */
    public List<BcSendPlatformModel> getModelList() {
        return DBManager.getORMExecuter().getModelList(BcSendPlatformModel.class, null,null).getData();
    }

    public void save(BcSendPlatformModel model) {
        BcSendPlatformModel vmodel = null;
        if (model.getPlatformId() != null) {
            vmodel = DBManager.getORMExecuter().getModel(model);
        }
        if (vmodel == null) {
            DBManager.getORMExecuter().create(model);
        } 
        else {
            DBManager.getORMExecuter().update(model);
        }
    }

    /**
     * 获取去重的数据策略
     * 
     * @return
     */
    public Set<String> getPlatformSet() {

        IORMExecuter ormExecuter = DBManager.getORMExecuter();
        ModelPageResult mpr = ormExecuter.getModelList(BcSendPlatformModel.class, null,null);
        List<BcSendPlatformModel> models = mpr.getData();

        Set<String> platformSet = new HashSet<>();

        for (BcSendPlatformModel model : models) {
            String rtStId = model.getRtStId();
            String minStId = model.getMinStId();
            String hourStId = model.getHourStId();
            String dayStId = model.getDayStId();

            platformSet.add(rtStId);
            platformSet.add(minStId);
            platformSet.add(hourStId);
            platformSet.add(dayStId);
        }

        return platformSet;
    }


    /**
     * 获取指定发送平台
     *
     * @return
     */
    public BcSendPlatformModel getModel(BcSendPlatformModel model) {
        return DBManager.getORMExecuter().getModel(model);
    }
}